import pygame
import time
import random

# 初始化Pygame
pygame.init()

# 设置游戏窗口的尺寸
window_width = 600
window_height = 400

# 创建游戏窗口
game_window = pygame.display.set_mode((window_width, window_height))

# 设置窗口标题
pygame.display.set_caption('贪吃蛇游戏')

# 定义颜色
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)

# 设置游戏时钟
clock = pygame.time.Clock()

# 游戏结束标志
game_over = False

# 定义蛇的初始位置和大小
snake_position = [100, 50]
snake_body = [[100, 50], [90, 50], [80, 50]]

# 定义蛇的移动方向
direction = 'RIGHT'
change_to = direction

# 定义食物的位置
food_position = [random.randrange(1, (window_width//10)) * 10,
                 random.randrange(1, (window_height//10)) * 10]
food_spawn = True

# 显示得分
def show_score(choice, color, font, size):
    score_font = pygame.font.SysFont(font, size)
    score_surface = score_font.render('得分 : ' + str(len(snake_body) - 3), True, color)
    score_rect = score_surface.get_rect()
    if choice == 1:
        score_rect.midtop = (window_width / 10, 15)
    else:
        score_rect.midtop = (window_width / 2, window_height / 4)
    game_window.blit(score_surface, score_rect)

# 游戏主循环
while not game_over:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                change_to = 'UP'
            elif event.key == pygame.K_DOWN:
                change_to = 'DOWN'
            elif event.key == pygame.K_LEFT:
                change_to = 'LEFT'
            elif event.key == pygame.K_RIGHT:
                change_to = 'RIGHT'

    # 确保蛇不能直接反向移动
    if change_to == 'UP' and not direction == 'DOWN':
        direction = 'UP'
    if change_to == 'DOWN' and not direction == 'UP':
        direction = 'DOWN'
    if change_to == 'LEFT' and not direction == 'RIGHT':
        direction = 'LEFT'
    if change_to == 'RIGHT' and not direction == 'LEFT':
        direction = 'RIGHT'

    # 根据方向移动蛇头的位置
    if direction == 'UP':
        snake_position[1] -= 10
    if direction == 'DOWN':
        snake_position[1] += 10
    if direction == 'LEFT':
        snake_position[0] -= 10
    if direction == 'RIGHT':
        snake_position[0] += 10

    # 移动蛇
    snake_body.insert(0, list(snake_position))
    if snake_position[0] == food_position[0] and snake_position[1] == food_position[1]:
        food_spawn = False
    else:
        snake_body.pop()

    if not food_spawn:
        food_position = [random.randrange(1, (window_width//10)) * 10,
                         random.randrange(1, (window_height//10)) * 10]
    food_spawn = True

    # 填充背景颜色
    game_window.fill(black)

    # 在游戏窗口中绘制蛇
    for pos in snake_body:
        pygame.draw.rect(game_window, white, pygame.Rect(pos[0], pos[1], 10, 10))

    # 绘制食物
    pygame.draw.rect(game_window, white, pygame.Rect(food_position[0], food_position[1], 10, 10))

    # 检查蛇是否撞到边界
    if snake_position[0] < 0 or snake_position[0] > window_width-10:
        game_over = True
    if snake_position[1] < 0 or snake_position[1] > window_height-10:
        game_over = True

    # 检查蛇是否撞到自己
    for block in snake_body[1:]:
        if snake_position[0] == block[0] and snake_position[1] == block[1]:
            game_over = True

    # 显示得分
    show_score(1, white, 'times new roman', 20)

    # 刷新游戏屏幕
    pygame.display.update()

    # 控制游戏速度
    clock.tick(10)

# 游戏结束后的显示
def game_over_message():
    my_font = pygame.font.SysFont('times new roman', 50)
    game_over_surface = my_font.render('游戏结束', True, white)
    game_over_rect = game_over_surface.get_rect()
    game_over_rect.midtop = (window_width / 2, window_height / 4)
    game_window.blit(game_over_surface, game_over_rect)
    pygame.display.flip()
    time.sleep(2)
    pygame.quit()
    quit()

game_over_message()
